package com.dzxcqmxm.servlet.photo;

import com.dzxcqmxm.Dao.PhotoDao;
import com.dzxcqmxm.Dao.impl.PhotoDaoImpl;
import com.dzxcqmxm.JB.Photo;
import com.dzxcqmxm.Dao.impl.testDel.deleteFile;
import com.mysql.cj.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Payboi
 * @create 2022/6/4-21:37
 * @FileName delPhotoServlet
 * @Description
 */
@WebServlet("/delPhotoServlet")
public class delPhotoServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        /**
         * 这是删除数据库数据
         */
        PhotoDao photoDao = new PhotoDaoImpl();
        Photo photo = new Photo();
        try {
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=utf-8");
            //获得表单数据
            photo.setPhotoName(new String(request.getParameter("PhotoName" ).getBytes("iso-8859-1"),"UTF-8"));

            System.out.println("打印一下获取的删除的表单数据："+photo.getPhotoName());

            try {
                photoDao.delPhoto(photo.getPhotoName());
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        /**
         * 把本机照片也删掉,得获取路径
         * 这是yhDisplayPhoto.jsp页面过来的
         *
         * 这里实现的是动态的删除，我想直接实习静态的，去隔壁页面
         */
        String XTXCName = (String) request.getSession ().getAttribute ("XTXCName");
        String AlbumUrl = (String) request.getSession ().getAttribute ("AlbumUrl");
        String AlbumName = (String) request.getSession ().getAttribute ("AlbumName");

        System.out.println("测试是否来到了这里");
        //获取那个文件的后缀名

        StringBuilder good = new StringBuilder(AlbumUrl);
        System.out.println("原来的字符串："+good);

        if(StringUtils.isNullOrEmpty(good.toString())&&good.toString().length()>0){
            String name3 = good.toString().substring(good.toString().lastIndexOf("."),good.toString().length()-1);
            System.out.println("打印一下照片后缀名：name3："+name3);
            String dir = "D:/电子相册/"+XTXCName+"/"+AlbumName+"/"+photo.getPhotoName()+name3;
            deleteFile.deleteFile(dir);
        }else{
            String name3 = good.toString().substring(good.toString().lastIndexOf("."),good.toString().length()-1);
            System.out.println("打印一下照片后缀名：name3："+name3);
            String dir = "D:/电子相册/"+XTXCName+"/"+AlbumName+"/"+photo.getPhotoName()+name3;
            deleteFile.deleteFile(dir);
            System.out.println("下面执行好了");


        }

        /**
         *
         * result.put("value", valueBuilder.toString().substring(0,valueBuilder.toString().length()-1));
         *
         * if (StringUtils.isNotBlank(valueBuilder.toString()) && valueBuilder.toString().length() >0){
         *                             result.put("value", valueBuilder.toString().substring(0,valueBuilder.toString().length()-1));
         *                         }
         */




        response.sendRedirect("/dzxc/YongHu/yhPhoto/yhDisplayPhoto.jsp");

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
